<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>智慧社区后台</title>
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
<link rel="stylesheet" href="${pageContext.request.contextPath}/plugins/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/AdminLTE.min.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/plugins/bootstrap-table/css/bootstrap-table.css">
<link rel="stylesheet" href="${pageContext.request.contextPath}/plugins/zTree/v3/css/metroStyle/metroStyle.css" type="text/css">
</head>
<style>
</style>
<body style="padding-bottom: 90px; background-color: #ECF0F5;">
	<section class="content" style="margin: 15px; background-color: white;">
		<div class="row">
			<div class="col-sm-12">
				<table id="table">
					<thead style="background-color: #00C0EF">
						<tr style="background-color: #00C0EF">
							<!-- 							<th class="text-center" data-field=""></th> -->
							<th class="text-center" data-field="roleName">角色名称</th>
							<th class="text-center" data-field="roleDescription">角色描述</th>
							<th class="text-center" data-field="id">操作</th>
						</tr>
					</thead>
				</table>
			</div>
		</div>
	</section>
	<!-- Large modal -->
	<div id="modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
		<div class="modal-dialog" role="document">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal" aria-label="Close">
						<span aria-hidden="true">&times;</span>
					</button>
					<h4 class="modal-title">角色编辑</h4>
				</div>
				<div class="modal-body">
					<div class="form-group">
						<label for="role_name" class="control-label">角色名:</label> <input type="text" class="form-control" id="role_name" onchange="checkAccount(this)">
					</div>
					<!-- 					<div class="form-group"> -->
					<!-- 						<label for="role_name" class="control-label">所属部门:</label> <select id="community_name" class="form-control"> -->
					<!-- 						</select> -->
					<!-- 					</div> -->
					<div class="form-group">
						<label class="control-label">角色权限：</label>
						<div>
							<ul id="menus" name="menus" class="ztree"></ul>
						</div>
					</div>
					<div class="form-group">
						<label class="control-label">角色描述：</label>
						<textarea rows="5" style="resize: none;" class="form-control" id="role_description" name="role_description"></textarea>
					</div>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
					<button type="button" class="btn btn-primary" onclick="submitRole();">提交</button>
				</div>
			</div>
		</div>
	</div>
</body>
<script src="${pageContext.request.contextPath}/plugins/jQuery/jquery-2.2.3.min.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap/js/bootstrap.min.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-zh-CN.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-export.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/tableExport.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-toolbar.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-filter-control.js"></script>
<script src="${pageContext.request.contextPath}/plugins/zTree/v3/js/jquery.ztree.all-3.5.min.js"></script>
<!-- layer -->
<script src="${pageContext.request.contextPath}/plugins/layer/2.4/layer.js"></script>
<script>
	var oldRoleName;
	var roleId = "";
	var isChecked = true;
	var zTree;
	var setting = {
		data : {
			key : {
				children : 'children',
				name : 'menuName'
			}
		},
		check : {
			autoChickTrigger : true,
			enable : true,
			chkStyle : "checkbox",
			chkboxType : {
				"Y" : "ps",
				"N" : "ps"
			}
		},
		view : {
			txtSelectedEnable : true
		}
	};
	function actionFormatter(value, row, index) {
		return [ "<button class='btn btn-primary btn-xs glyphicon glyphicon-pencil' onclick='showModal(\"" + row.roleId + "\");'>编辑</button>",
				"&nbsp;<button class='btn btn-danger btn-xs glyphicon glyphicon-trash' onclick='delRole(\"" + row.roleId + "\");'>删除</button>", ].join('');
	}
	var table_dataset = [ {
		field : 'roleName',
		align : 'center',
	}, {
		field : 'roleDescription',
		align : 'center',
	}, {
		align : 'center',
		width : 150,
		formatter : actionFormatter,
	} ];
	$('#table').bootstrapTable({
		url : "${pageContext.request.contextPath}/selectRoles",
		idField : "id",
		columns : table_dataset,
		striped : true, //是否显示行间隔色
		pagination : true, //是否显示分页
		pageSize : 10, //每页的记录行数
		pageList : [ 10, 25, 50, 100 ], //可供选择的每页的行数
		search : false, //是否显示搜索
		showRefresh : true,
		sidePagination : "server", //表示服务端请求  
		queryParamsType : "limit",
		queryParams : function queryParams(params) { //设置查询参数  
			var param = {
				offset : params.offset,
				limit : params.limit
			};
			return param;
		},
		toolbar : "<button class='btn btn-primary glyphicon glyphicon-plus' onclick='showModal(\"" + '' + "\");'>新增数据</button>",
		// 						+"&nbsp;<button class='btn btn-danger glyphicon glyphicon-trash'>批量删除</button>",
		icons : {
			paginationSwitchDown : 'glyphicon-collapse-down icon-chevron-down',
			paginationSwitchUp : 'glyphicon-collapse-up icon-chevron-up',
			refresh : 'glyphicon-refresh icon-refresh',
			toggle : 'glyphicon-list-alt icon-list-alt',
			columns : 'glyphicon-th icon-th',
			detailOpen : 'glyphicon-plus icon-plus',
			detailClose : 'glyphicon-minus icon-minus',
			"export" : 'glyphicon-export icon-share'
		},
		showExport : true,
		exportDataType : 'basic',
		exportTypes : [ 'json', 'xml', 'csv', 'txt', 'sql', 'excel' ]
	});

	$(function() {
		select_data();
		$.ajax({
			url : "${pageContext.request.contextPath}/selectAllCommunityList",
			type : "get",
			dataType : "json",
			success : function(data) {
				var list = data.list;
				var communityName = "<option value='无' selected>请选择...</option>";
				for (var i = 0; i < list.length; i++) {
					communityName += "<option value='"+list[i].organizationName+"'>" + list[i].organizationName + "</option>";
				}
				$("#community_name").html(communityName);
			},
			error : function() {
			}
		});
	});

	function showModal(id) {
		if (id == '0084ae3f-3764-48d1-989c-0e9241e178a9') {
			layer.alert("超级管理员不能修改!");
			return;
		}
		zTree.checkAllNodes(false);
		if (id == "") {
			oldRoleName = '';
			roleId = '';
			$("#role_name").val('');
			$("#role_description").val('');
			$("#community_name").val('无');
			$('#modal').modal({
				backdrop : 'static',
				keyboard : false
			});
		} else {
			roleId = id;
			$.ajax({
				url : "${pageContext.request.contextPath}/selectRoleById",
				type : "get",
				data : {
					roleId : roleId
				},
				dataType : "json",
				success : function(data) {
					oldRoleName = data.roleName;
					$("#role_name").val(data.roleName);
					$("#role_description").val(data.roleDescription);
					$("#community_name").val(data.communityName);
					var menuSet = data.menuSet;
					if (menuSet && menuSet != '') {
						menuSet = menuSet.split("+");
						var nodes = zTree.getCheckedNodes(false);
						console.log(nodes)
						for (var i = 0; i < menuSet.length; i++) {
							for (var j = 0; j < nodes.length; j++) {
								if (nodes[j].menuCode === menuSet[i]) {
									zTree.checkNode(nodes[j], true, false)
								}
							}
						}
					}
					$('#modal').modal({
						backdrop : 'static',
						keyboard : false
					});
				},
				error : function() {
					layer.msg('系统异常!', {
						icon : 2,
						time : 800
					});
				}
			});
		}
	}

	function select_data() {
		$.ajax({
			url : "${pageContext.request.contextPath}/selectMenuTreeAll",
			type : "get",
			dataType : "json",
			async : false,
			success : function(data) {
				$(document).ready(function() {
					$.fn.zTree.init($("#menus"), setting, data);
					zTree = $.fn.zTree.getZTreeObj("menus");
					zTree.expandAll(true);
				});
			},
			error : function() {
				layer.msg('系统异常!', {
					icon : 2,
					time : 800
				});
			}
		});
	}

	function checkAccount(obj) {
		$.ajax({
			url : "${pageContext.request.contextPath}/checkRoleName",
			type : "post",
			async : false,
			data : {
				roleName : obj.value
			},
			dataType : "json",
			success : function(data) {
				if (data.result < 0 && obj.value != oldRoleName) {
					isChecked = false;
				} else {
					isChecked = true;
				}
			},
			error : function() {
			}
		});
	}

	function submitRole() {
		var roleName = $('#role_name').val();
		var communityName = $("#community_name").val();
		var roleDescription = $('#role_description').val();
		if (roleName === null || roleName === '' || communityName === null || communityName === '' || roleDescription === null || roleDescription === '') {
			layer.alert("新增的数据中填写不完整!");
			return false;
		}
		if (!isChecked) {
			layer.alert("角色名已被使用!");
			return false;
		}
		var menuSet = '';
		var nodes = zTree.getCheckedNodes();
		$.each(nodes, function(k, v) {
			menuSet += v.menuCode + '+';
		});
		if (nodes.length > 0) {
			menuSet = menuSet.substring(0, menuSet.length - 1)
		}
		var url = roleId === '' ? '${pageContext.request.contextPath}/insertRole' : '${pageContext.request.contextPath}/updateRole';
		$.ajax({
			url : url,
			type : "post",
			data : {
				roleId : roleId,
				roleName : roleName,
				roleDescription : roleDescription,
				menuSet : menuSet,
				communityName : communityName
			},
			dataType : "json",
			success : function(data) {
				var result = data['result'];
				if (result > 0) {
					layer.msg('操作成功!', {
						icon : 1,
						time : 800
					});
					setTimeout(function() {
						$("#table").bootstrapTable('refresh');
						$("#modal").modal("hide");
					}, 1000);
				} else {
					layer.msg('操作失败!', {
						icon : 2,
						time : 800
					});
				}
			},
			error : function() {
				layer.msg('系统繁忙，请稍后再试!', {
					icon : 2,
					time : 800
				});
			}
		});

		return false;
	}

	function delRole(id) {
		if (id == '0084ae3f-3764-48d1-989c-0e9241e178a9') {
			layer.alert("超级管理员不能删除!");
			return;
		}
		layer.confirm("是否要删除角色？", function(index) {
			$.ajax({
				url : "${pageContext.request.contextPath}/deleteRole",
				type : "post",
				data : {
					roleId : id
				},
				dataType : "json",
				success : function(data) {
					if (data.result === 1) {
						layer.msg('操作成功!', {
							icon : 1,
							time : 800
						});
						setTimeout(function() {
							$("#table").bootstrapTable('refresh');
						}, 500);
					} else {
						layer.msg('操作失败!', {
							icon : 2,
							time : 800
						});
					}
				},
				error : function() {
					layer.msg('系统繁忙，请稍后再试!', {
						icon : 5,
						time : 800
					});
				}
			});
		});
	}
</script>
</html>
